<script>
  //7
  // 改写哪里使这段代码的结果能如预想的运行？
  const beNice = () => {
    throw new Error('我的状态良好')
  }
  Promise.resolve()
    .then(beNice)
    .catch((niceMessage) => console.log(niceMessage) /* 应打印“我的状态良好” */)

  //8
  // 改写哪里使这段代码的结果能如预想的运行？
  const removeMultipleSpaces = (s) => s.replace(/\s+/g, ' ')
  const wordsPromise = Promise.resolve('每   个 字   之  间  有  一 个  空   格  ')
  wordsPromise
    .then((s) => removeMultipleSpaces(s))
    .then((actual) => {
      console.log(actual) /*应打印 '每 个 字 之 间 有 一 个 空 格'*/
    })

  //9
  const appendPeriod = (s) => `${s}。`
  const wordsPromise9 = Promise.resolve('一     句  话   没 有  结   束')
  wordsPromise9
    // ？？？如何改写使结果如预想运行？
    .then((s) => appendPeriod(`${s}`))
    .then((actual) => {
      console.log(actual) // 应打印'一 句 话 没 有 结 束。'
    })

  // 10
  const asyncUpperCaseStart = (s, onDone) => {
    const format = () => onDone(s[0].toUpperCase() + s.substr(1))
    setTimeout(format, 100)
  }

  const wordsPromise10 = Promise.resolve('sentence without an end')
  wordsPromise10
    .then((s) => new Promise((resolve) =>  asyncUpperCaseStart(s, resolve))) // <--如何修改这一句？
    .then((s) => new Promise((resolve) => setTimeout(() => resolve(appendPeriod(s)), 100)))
    .then((actual) => {
      console.log(actual) // 应打印'Sentence without an end。'
    })

  //11
  const trim = s => s.replace(/^\s+/, '').replace(/\s+$/, '');
  const wordsPromise11 = Promise.resolve('trailing space   ')
  wordsPromise11
    .then((s) => new Promise((resolve) => asyncUpperCaseStart(s, resolve)))
    .then((s) => new Promise((resolve) => setTimeout(() => resolve(appendPeriod(s)), 100)))
    .then((s) => new Promise((resolve) => setTimeout(() => resolve(trim(s)), 100)))
    .then((actual) => console.log(actual)) //应该打印 'Trailing space。'
    //  修改哪里能使结果如上一行打印的结果？

  //12
  Promise.resolve('hello world').then((w) => console.log(w)) // 这个例子打印 'hello world'
  // Promise.resolve(v)的作用是直接生成一个成功状态的promise对象，并把v作为promise对象的值
  // 请你直接写一个Promise.myResolve方法，实现类似的效果
  Promise.myResolve = function (v) {
    //???
    return new Promise(resolve=>{
      resolve('hello world')
    })
  }
  Promise.myResolve('hello world').then((w) => console.log(w))

  // 13
  // 如何才能让代码按期待的方式运行？
  const promise = Promise.reject()
  promise
    .then(() => {
      console.log('不应该进入这个回调')
    })
    .catch(() => {
      console.log('应该进入这里运行')
    })
</script>
